package com.atguigu.system.controller;

import com.atguigu.common.helper.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.common.util.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.LoginVo;
import com.atguigu.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "处理前端登录")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

//    @ApiOperation("登录")
//    @PostMapping("/login")
//    public Result login(){
//        //{"code":200,"data":{"token":"admin-token"}}
//        //创建一个Map
//        Map map = new HashMap<>();
//        map.put("token","admin-token");
//        return Result.ok(map);
//    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        //获取用户名和密码
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        //调用SysUserService中根据用户名查询用户信息的方法
        SysUser sysUser = sysUserService.getSysUserByUsername(username);
        //判断用户是否存在
        if(sysUser == null){
            //根据用户名查询不到对应的记录
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //判断用户输入的密码是否正确
        if(!MD5.encrypt(password).equalsIgnoreCase(sysUser.getPassword())){
            //用户输入的密码不正确
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //判断用户是否被停用
        if(sysUser.getStatus() == 0){
            //证明该账号已停用
            return Result.build(null,ResultCodeEnum.ACCOUNT_STOP);
        }
        //通过JwtHelper工具类生成Token
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        //创建一个Map
        Map map = new HashMap<>();
        map.put("token",token);
        return Result.ok(map);
    }

//    @ApiOperation("获取用户信息")
//    @GetMapping("/info")
//    public Result getInfo(){
//        //{"code":200,"data":{"roles":["admin"],"introduction":"I am a super administrator","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif","name":"Super Admin"}}
//        //创建一个Map
//        Map map = new HashMap();
//        //设置用户姓名
//        map.put("name","Admin");
//        //设置用户的头像
//        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        return Result.ok(map);
//    }


    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public Result getInfo(HttpServletRequest request){
        //获取请求头中的token
        String token = request.getHeader("token");
        //根据token获取用户的id
        Long userId = JwtHelper.getUserId(token);
        //调用SysUserService中根据用户id获取用户信息的方法
        Map<String,Object> map = sysUserService.getUseInfoByUserId(userId);
        return Result.ok(map);
    }


    @ApiOperation("登出")
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }
}
